// check to see if page is loaded before running code
 $(function() {
	
	buildClassList = function() {};
	
	$.extend(buildClassList.prototype, {

		init:function(){
			
			var classTitles = [];
			var classTimes = [];
			var eventData = [];
			var startTimes = [];

			var eventEntry = "";
			var startTime = "";
			var startDate = new Date();
			var previousStartDate = "";
			var html = "";
			
			var that = this;
		
			//singleevents=true : set recurring events to be represented as single events
			$.getJSON("http://www.google.com/calendar/feeds/1phillypoweryoga%40gmail.com/public/full?alt=json-in-script&callback=?&orderby=starttime&max-results=43&singleevents=true&sortorder=ascending&futureevents=true",

		        function(json){	

					console.log(json);

					html = '<p>Location ' + json.feed.gd$where.valueString + '</p>';

		            html += '<ul>';

		            $.each(json.feed.entry,function(i,entry){

						className = entry.title.$t;

						 if (entry['gd$when'][0].startTime != undefined) {

								eventEntry = entry['gd$when'][0].startTime;

								eventData = eventEntry.split(/[T]/);

								startTime = eventData[1].slice(0,5);

								startDate = eventData[0] + "";
								
								startDate = startDate.replace( new RegExp( "-", "g" ), "/" );
								
								startDate = dateFormat(startDate,"fullDate");
								
								console.log(dateFormat(startDate,"fullDate"));
								
								startTime = that.formatTime(startTime);

								startTimes.push(startTime);

						 } else {

								console.log("no entry ");

						 }

						//modify this for use in schedule
						if(startDate != previousStartDate)
								{	
									
									html += "</ol><h3 class='date-label'>" + startDate + "</h3>";

									html += "<ol class='recList'>";
								}

						previousStartDate = startDate;

						if(i%2==0){
							rowClass = "even";
						}
						else{
							rowClass = "odd";
						}		

		    			html += '<li><span class="classTime">' + startTime + '</span><span class="className"> ' + className + '</span>';

						classTitles.push(entry.title.$t);

		            }); //$.each

		            html += '</ol>';

		            $('#outputDiv').html(html);
		        
					} //function(json)

		    )//$.getJSON
		
		}, //init
		
		formatTime: function(startingTime) {
			
			var startTimeHour = "";
			var startTimeMinutes = "";
			var setAmPm = "";
			
			startTimeHour = startingTime.slice(0,2);
			
			startTimeMinutes = startingTime.slice(3,5);
			
			if (startTimeHour > 12) {

				setAmPm = "pm";

				//convert startTime to string	
				startTimeHour = startTimeHour - 12 + "";
				
				startingTime = startTimeHour + ":" + startTimeMinutes + setAmPm;

			} else {

				setAmPm = "am";

				startTimeHour = startingTime.slice(0,2);

				startingTime = startTimeHour + ":" + startTimeMinutes + setAmPm;

			}
			
			console.log("Time: " + startingTime);
			
			//console.log("Formatted Time: " + startingTime);
			
			return startingTime;
			
		}//formatTime
	
	}); //$.extend(buildClassList.prototype,
	
	var classSchedule = new buildClassList();
	
	classSchedule.init();
	
}); // $(function() 

